package com.study.talk.ui.app.base

import android.annotation.SuppressLint
import android.content.ContentValues
import android.net.Uri
import android.os.Bundle
import android.os.Environment
import android.provider.MediaStore
import android.text.TextUtils
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import android.widget.TextView
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.PickVisualMediaRequest
import androidx.activity.result.contract.ActivityResultContracts.PickVisualMedia.ImageOnly
import androidx.fragment.app.Fragment
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.study.talk.R
import com.study.talk.App
import com.study.common.ToastUtil
import com.study.common.annotation.Desc

open class BaseFragment : Fragment() {

    companion object{
        private const val TAG = "BaseFragment"
    }

    var uri: Uri? = null

    fun checkInput(textView: EditText, toast: String): Boolean {
        if (TextUtils.isEmpty(editTrim(textView))) {
            ToastUtil.showToast(context = requireContext(), toast)
            return true
        }
        return false
    }

    fun textCheckToast(text: String? ,msg :String) : Boolean{
        if (text.isNullOrEmpty()){
            ToastUtil.showToast(context = requireContext(),msg)
        }
        return text.isNullOrEmpty()
    }


    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        Log.d(TAG, "onCreateView: ")
        return super.onCreateView(inflater, container, savedInstanceState)
    }

    fun setText(editText: EditText, text: String?) {
        editText.setText(text)
    }


    @Desc("输入框去空格")
    private fun editTrim(text: EditText): String {
        return text.text.toString().trim { it <= ' ' }
    }

    @Desc("显示底部选择图像弹窗，由用户选择拍照或选择图像")
    @SuppressLint("InflateParams")
    fun showSelectPhotoDialog(
        selectPhotoLauncher: ActivityResultLauncher<PickVisualMediaRequest>,
        takePhotoLauncher: ActivityResultLauncher<Uri>
    ) {
        val dialog = BottomSheetDialog(requireActivity())

        val bottomDialogView =
            LayoutInflater.from(requireActivity()).inflate(R.layout.dialog_bottom, null)
        val takePhoto = bottomDialogView.findViewById<TextView>(R.id.take_photo)
        val selectPhoto = bottomDialogView.findViewById<TextView>(R.id.select_photo)
        val cancel = bottomDialogView.findViewById<TextView>(R.id.cancel)

        //点击拍照
        takePhoto.setOnClickListener { _ ->
            takePhoto(takePhotoLauncher)
            dialog.dismiss()
        }

        //点击相册
        selectPhoto.setOnClickListener { _ ->
            selectPhoto(selectPhotoLauncher)
            dialog.dismiss()
        }

        //点击取消 弹窗消失
        cancel.setOnClickListener { _ -> dialog.dismiss() }

        dialog.setContentView(bottomDialogView)
        dialog.show()
    }

    @Desc("拍照并换返回")
    private fun takePhoto(takePhotoLauncher: ActivityResultLauncher<Uri>) {
        val contentValues = ContentValues()
        contentValues.put(
            MediaStore.MediaColumns.DISPLAY_NAME, "IMG_${System.currentTimeMillis()}.jpg"
        )
        contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_PICTURES)
        uri = requireActivity().contentResolver.insert(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues
        )
        uri?.let { takePhotoLauncher.launch(it) }
    }

    @Desc("选择图片")
    private fun selectPhoto(selectPhotoLauncher: ActivityResultLauncher<PickVisualMediaRequest>) {
        selectPhotoLauncher.launch(
            PickVisualMediaRequest.Builder().setMediaType(ImageOnly).build()
        )
    }

    val app: App
        get() = requireActivity().application as App
}
